

import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
//获取柱状图option
function getBarOption(xAxisData, series = [], title = '', subTitle = '', showLegend='1',nowfontsize,tiltShow='0'){
    let rotate = 0;
    if(tiltShow == '1'){
        rotate = 45;
    }
    series = series.map(item => {
        item.itemStyle = {
            normal: {
                label: {
                    show: true,//是否展示
                    position: 'top'
                } ,
            },
        }
        return item;
    })
    let legendData = series.map(item=>{
        item.type = 'bar';
        return item.name;
    });
    let option = {
        title: {
            text: title,
            subtext: subTitle,
            textStyle:{
                fontSize: nowfontsize,
            }
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            textStyle:{
                fontSize: nowfontsize,
            }
        },
        legend: {
            show: showLegend == '1',
            data:legendData, 
            textStyle:{
                fontSize: nowfontsize,
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
            tooltip: {
                textStyle:{
                    fontSize: nowfontsize,
                }
            }
        },
        yAxis: {
            type: 'value',
            boundaryGap: [0, 0.01],
            axisLabel:{
                fontSize: nowfontsize,
            }
        },
        xAxis: {
            type: 'category',
            data: xAxisData,
            axisLabel:{
                rotate: rotate,
                fontSize: nowfontsize,
            }
        },
        series: series,
        textStyle:{
            fontSize: nowfontsize,
        }
    }
   
    return option;
}

//获取柱状图option
function getBar3dOption(xAxisData, series, title = '', subTitle = '', showLegend='1',nowfontsize,tiltShow='0'){
    let rotate = 0;
    if(tiltShow='1'){
        rotate = 45;
    }
    let dimensions = ['xName'];
    let source = [];
    let encode = {x:'xName'};
    let tooltip = [0];
    series.map((item, i)=>{
        if(i == 0 && !encode.y) encode.y = item.name;
        if(i == 1 && !encode.z) encode.z = item.name;
        tooltip.push(i+1);
        dimensions.push(item.name);
    });
    encode.tooltip = tooltip;
    xAxisData.map((xName, i)=>{
        let arr = [];
        arr.push(xAxisData[i]);
        series.map((item, j)=>{
            arr.push(item.data[i]);
        });
        source.push(arr);
    });
    let option = {
        grid3D: {
            axisPointer:{
                label:{
                    textStyle: {
                         fontSize: nowfontsize,
                    }
                }
            },
            axisLabel:{
                textStyle: {
                    fontSize: nowfontsize
                },
            }
        },
        tooltip: {
            textStyle:{
                fontSize: nowfontsize,
            }
        },
        xAxis3D: {
            type: 'category',
            axisLabel:{
                textStyle: {
                    rotate: rotate,
                    fontSize: nowfontsize
                },
            }
        },
        yAxis3D: {
            type: 'category',
            axisLabel:{
                textStyle: {
                    fontSize: nowfontsize
                },
            }
        },
        zAxis3D: {
            axisLabel:{
                textStyle: {
                    fontSize: nowfontsize
                },
            }
        },
        visualMap: {
            show: showLegend == '1',
            max: 1e8,
            dimension: 'Population',
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        dataset: {
            dimensions,
            source
        },
        series: [
            {
                type: 'bar3D',
                // symbolSize: symbolSize,
                shading: 'lambert',
                encode
            }
        ],
        textStyle:{
            fontSize: nowfontsize,
        }
    };
    return option;
}
    
function getLineOption(xAxisData, series, title = '', subTitle = '', showLegend='1',nowfontsize,tiltShow='0'){
    let rotate = 0;
    if(tiltShow == '1'){
        rotate = 45;
    }
    let legendData = series.map(item=>{
        item.type = 'line';
        return item.name;
    });
    let option = {
        title: {
            text: title,
            textStyle:{
                fontSize: nowfontsize,
            }
        },
        tooltip: {
            trigger: 'axis',
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        legend: {
            show: showLegend == '1',
            data:legendData,
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
            tooltip: {
                textStyle:{
                    fontSize: nowfontsize,
                }
            }
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xAxisData,
            axisLabel:{
                rotate: rotate,
                fontSize: nowfontsize,
            }
        },
        yAxis: {
            type: 'value',
            axisLabel:{
                fontSize: nowfontsize,
            }
        },
        series: series ,
        textStyle:{
            fontSize: nowfontsize,
        }
    };
    return option;
}

function getAreaOption(xAxisData, series =  [], title = '', subTitle = '', showLegend='1',nowfontsize,tiltShow='0'){
    let rotate = 0;
    if(tiltShow =='1'){
        rotate = 45;
    }
    let legendData = series.map(item=>{
        item.type = 'line';
        item.stack =  getLabel(388005,"总量");
        item.areaStyle = {normal: {}};
        return item.name;
    });
    let option = {
        title: {
            text: title,
            textStyle:{
                 fontSize: nowfontsize,
             }
        },
        tooltip : {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            },
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        legend: {
            show: showLegend=='1',
            data: legendData,
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
            tooltip: {
                textStyle:{
                    fontSize: nowfontsize,
                }
            }
        },
        xAxis : [
            {
                type : 'category',
                boundaryGap : false,
                data : xAxisData.Number,
                axisLabel:{
                    rotate: rotate,
                    fontSize: nowfontsize,
                }
            }
        ],
        yAxis : [
            {
                type : 'value',
                axisLabel:{
                    fontSize: nowfontsize,
                }
            }
        ],
        series :series,
        textStyle:{
            fontSize: nowfontsize,
        }
    };
    return option;
}


function getBarGraphOption(xAxisData, series = [], title = '', subTitle = '', showLegend='1',nowfontsize,tiltShow='0'){
    let rotate = 0;
    if(tiltShow == '1'){
        rotate = 45;
    }
    let legendData = series.map(item=>{
        item.type = 'bar';
        item.itemStyle = {
            normal: {
                label: {
                    show: true,//是否展示
                    position: 'right'
                } ,
            },
        }
        item.data = item.data.reverse();
        return item.name;
    });
    let option = {
        title: {
            text: title,
            subtext: subTitle,
            textStyle:{
                 fontSize: nowfontsize,
             }
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        legend: {
            show: showLegend=='1',
            data: legendData.reverse(),
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
            tooltip: {
                textStyle:{
                    fontSize: nowfontsize,
                }
            }
        },
        xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01],
            axisLabel:{
                rotate: rotate,
                fontSize: nowfontsize,
            }
        },
        yAxis: {
            type: 'category',
            data: xAxisData.reverse(),
            axisLabel:{
                fontSize: nowfontsize,
            }
        },
        series: series,
        textStyle:{
            fontSize: nowfontsize,
        }
    }
    return option;
}

function getPieOption(series = {}, title = '', subTitle = '', showLegend='1',nowfontsize){
    let data = [];
    let legendData = series.data.map(item=>{
        data.push({
            name: item[0],
            value: item[1]
        });
        return item[0];
    });
    let option = {
        title : {
            text: title,
            subtext: subTitle,
            x:'center',
            textStyle:{
                 fontSize: nowfontsize,
             }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '-5%',
            containLabel: true,
        },
        tooltip : {
            trigger: 'item',
            formatter: "{b} : {d}%",
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        legend: {
            show: showLegend=='1',
            orient: 'vertical',
            left: 'left',
            data:legendData,
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        series : [
            {
                type: 'pie',
                radius : '55%',
                center: ['50%', '60%'],
                data: data,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ],
        textStyle:{
            fontSize: nowfontsize,
        }
    };
    return option;
}


function getCircleOption(series = {}, title = '', subTitle = '', showLegend='1',nowfontsize){
    let data = [];
    let legendData = series.data.map(item=>{
        data.push({
            name: item[0],
            value: item[1]
        });
        return item[0];
    });
    let option = {
        title : {
            text: title,
            subtext: subTitle,
            x:'center',
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '-5%',
            containLabel: true,
        },
        tooltip: {
            trigger: 'item',
            formatter: "{b}: {c} ({d}%)",
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        legend: {
            show: showLegend=='1',
            orient: 'vertical',
            x: 'left',
            data: legendData,
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        series: [
            {
                type:'pie',
                radius: ['10%', '80%'],
                avoidLabelOverlap: false,
                label: {
                    normal: {
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
                        show: false,
                        textStyle: {
                            fontSize: '30',
                            fontWeight: 'bold'
                        }
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data:data
            }
        ]
    };
    return option;
}

function getGaugeOption(datas, title = '', subTitle = '', showLegend='1',nowfontsize){
    const { series } = datas;
    const { data } = series;
    const { startval, middleval, realval, endval } = data;
    const myIsNaN = (value, def = 0) => {
        value = Number(value);  
        if(typeof value === 'number' && isNaN(value)){
            return def;
        }
        return value; 
    }
    let option = {
        grid: {
            left: '3%',
            right: '4%',
            bottom: '-5%',
            containLabel: true,
        },
        tooltip : {
            formatter: "{a} <br/>{c} {b}",
            textStyle:{
                 fontSize: nowfontsize,
            }
        },
        toolbox: {
            show: true,
            feature: {
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        series : [
            {
                type: 'gauge',
                z: 3,
                min: myIsNaN(startval,0),
                max: myIsNaN(endval,1),
                splitNumber: 10,
                radius: '50%',
                axisLine: {            // 坐标轴线
                    lineStyle: {       // 属性lineStyle控制线条样式
                        width: 10
                    }
                },
                axisTick: {            // 坐标轴小标记
                    length: 15,        // 属性length控制线长
                    lineStyle: {       // 属性lineStyle控制线条样式
                        color: 'auto'
                    }
                },
                splitLine: {           // 分隔线
                    length: 20,         // 属性length控制线长
                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                        color: 'auto'
                    }
                },
                axisLabel: {
                    backgroundColor: 'auto',
                    borderRadius: 2,
                    color: '#eee',
                    padding: 3,
                    textShadowBlur: 2,
                    textShadowOffsetX: 1,
                    textShadowOffsetY: 1,
                    textShadowColor: '#222'
                },
                title : {
                    // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                    fontWeight: 'bolder',
                    fontSize: 20,
                    fontStyle: 'italic'
                },
                // data: [{value: myIsNaN(realval, 0), name: '完成率'}]
                data: [{value: myIsNaN(realval, 0)}]
            }
        ]
    };
    return option;
}



function getEChartOption(data,nowfontsize){
    const { charttype, showLegend, tiltShow } = data;
    let { categories, dot, series, title, subTitle } = data;
    switch(charttype){
        case 'bar':
            return getBarOption(categories, series, title, subTitle, showLegend,nowfontsize,tiltShow);
            break;
        case 'bar3d':
            return getBar3dOption(categories, series, title, subTitle, showLegend,nowfontsize,tiltShow);
            break;
        case 'line':
            return getLineOption(categories, series, title, subTitle, showLegend,nowfontsize,tiltShow);
            break;
        case 'area':
            return getAreaOption(categories, series, title, subTitle, showLegend,nowfontsize,tiltShow);
            break;
        case 'bar-graph':
            return getBarGraphOption(categories, series, title, subTitle, showLegend,nowfontsize,tiltShow);
            break;
        case 'pie':
            return getPieOption(series, title, subTitle, showLegend,nowfontsize);
            break;
        case 'circle':
            return getCircleOption(series, title, subTitle, showLegend,nowfontsize);
            break;
        case 'gauge':
            return getGaugeOption(data, title, subTitle,nowfontsize);
            break;
    }
}
export default getEChartOption;
